package nu.aing.P20140609;

import nu.aing.utils.TestUtils;

/**
 * @author memekmaut@gmail.com
 */
public class BoxesDiv2 {
    
    public static void main(String[] args) {
        BoxesDiv2 obj = new BoxesDiv2();
        TestUtils.test(obj, "findSize", new int[] {8, 8});
        TestUtils.test(obj, "findSize", new int[] {5, 6});
        TestUtils.test(obj, "findSize", new int[] {1,1,13,1,1});
        TestUtils.test(obj, "findSize", new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32});
    }
    
    public int findSize(int[] candyCounts) {
        int b = 1, c = 0, i = 0;
        while(true) {
            if(i == candyCounts.length) return b;
            if((c += candyCounts[i++]) > b) {
                b = getBox(c);
            }
        }
    }
    
    private int getBox(int count) {
        int i = 1;
        while(true) {
            if((int) Math.pow(2, ++i) >= count) {
                break;
            }
        }
        return (int) Math.pow(2, i);
    }
}
